const rolesModel = require("../models/rolesModel")
const { validationResult } = require('express-validator');

// 角色添加
exports.addrole = (req, res) => {
    // validator表单验证
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
    // 其他内容
    let { name, description, rolecode } = req.body
    // 当前时间戳
    let createtime = Date.now()
    let p = rolesModel.createrole(name, description, createtime, rolecode)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色添加成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色添加失败！", error })
    })
}

// 角色删除--软删除
exports.deleterole_f = (req, res) => {
    let { id } = req.query
    let p = rolesModel.delete_f(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色禁用成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色禁用失败！", error })
    })
}

// 角色删除--软删除
exports.start = (req, res) => {
    let { id } = req.query
    let p = rolesModel.start_status(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色启用成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色启用失败！", error })
    })
}

// 角色删除--真删除
exports.deleterole_t = (req, res) => {
    let { id } = req.query
    let p = rolesModel.delete_t(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色删除成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色删除失败！", error })
    })
}
// 角色列表搜索--分页
exports.role_list_c = (req, res) => {
    // validator表单验证
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
    // 其他内容
    // 从请求中获取查询参数
    let { name, rolecode, page_size, page } = req.query;

    // 调用role_list函数获取角色列表
    rolesModel.role_list(name, rolecode, Number(page_size), Number(page))
        .then(roles => {
            // 将角色列表发送给客户端
            res.json({
                success: true,
                data: roles,
                message: '角色列表获取成功'
            });
        })
        .catch(error => {
            // 处理错误，将错误信息发送给客户端
            res.status(500).json({
                success: false,
                message: '角色列表获取失败',
                error: error.message
            });
        });
};
// 角色修改获取--get数据
exports.edit_get_c = (req, res) => {
    let { id } = req.query
    let p = rolesModel.edit_get(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色修改获取成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色修改获取失败！", error })
    })
}

// 角色权限获取--get数据
exports.permission_view = (req, res) => {
    let { id } = req.query
    let p = rolesModel.expression_view(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色权限获取成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色权限获取失败！", error })
    })
}

// 角色修改设置--set数据
exports.edit_set_c = (req, res) => {
    // validator表单验证
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
    // 其他内容
    let { id, name, description, rolecode } = req.body
    // 当前时间戳
    let updatetime = Date.now()
    let p = rolesModel.edit_set(id, name, description, updatetime, rolecode)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色修改设置成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色修改设置失败！", error })
    })
}
// 角色的权限设置
exports.expression_set_c = (req, res) => {
    let { id, expression_ids } = req.query
    let p = rolesModel.expression_set(id, expression_ids)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色权限的设置成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色权限的设置失败！", error })
    })
}
// 角色的列表-下拉菜单
exports.all_role = (req, res) => {
    let p = rolesModel.all_role()
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "角色的列表-下拉菜单成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "角色的列表-下拉菜单失败！", error })
    })
}